<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="/js/layui/css/layui.css" media="all">
    <script src="/js/layui/jquery.1.12.4.js" charset="utf-8"></script>
    <!-- 注意：如果你直接复制所有代码到本地，上述css路径需要改成你本地的 -->
</head>
<body>
<div class="demoTable" style="text-align:center">
    <div class="layui-inline">
        <div class="layui-inline layui-form">
            <#--            <label class="layui-form-label">搜索选择框</label>-->
            <div class="layui-inline">
                <select name="modules" id="key" lay-verify="required" lay-search="">
                    <option value="">点击选择班级</option>
                  <#list  allname  as  name>
                    <option  value=${name}  >
                        ${name}
                    </option>

                    </#list>
                </select>
            </div>
        </div>
    </div>

    <button class="layui-btn" data-type="reload">搜索</button>
</div>
<table class="layui-hide" id="test" lay-filter="test"></table>

<script type="text/html" id="toolbarDemo">
    <div class="layui-btn-container">
        <button class="layui-btn layui-btn-sm" lay-event="getCheckData">添加</button>
        <button class="layui-btn layui-btn-sm" lay-event="getCheckLength">获取选中数目</button>
        <button class="layui-btn layui-btn-sm" lay-event="isAll">验证是否全选</button>
    </div>
</script>

<script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>


<script src="/js/layui/layui.js" charset="utf-8"></script>
<!-- 注意：如果你直接复制所有代码到本地，上述js路径需要改成你本地的 -->

<script>
    layui.use('table', function () {
        var table = layui.table;

        table.render({
            elem: '#test'
            , url: '/admin/AllStudent'
            , toolbar: '#toolbarDemo'
            , title: '用户数据表'
            , cols: [[
                {type: 'checkbox', fixed: 'left'}
                , {field: 'id', title: '学生ID', width: 120, fixed: 'left', unresize: true, sort: true}
                , {field: 'classesname', title: '班次', width: 120, fixed: 'left', unresize: true, sort: true}
                , {field: 'username', title: '学生姓名', width: 120, }
                , {field: 'sex', title: '性别', width: 80, edit: 'text', sort: true}
                , {field: 'stage', title: '课程阶段', width: 120,  sort: true}
            ]]
            , page: true
            , id: 'testReload'
        });
        var $ = layui.$, active = {
            reload: function () {

                //执行重载
                table.reload('testReload', {
                     url: '/api/table/search'
                    ,page: {
                        curr: 1 //重新从第 1 页开始
                    }
                    , where: {
                         key1: $('#key').val()

                        // key1: $('#key').val()
                      //  ClassTimesId: '' ? '' : $('#xixi').val(),



                    }
                }, 'data');

            }
        };

        $('.demoTable .layui-btn').on('click', function () {
                var type = $(this).data('type');
                active[type] ? active[type].call(this) : '';

        })
        ;

        //头工具栏事件
        table.on('toolbar(test)', function (obj) {
            var checkStatus = table.checkStatus(obj.config.id);
            switch (obj.event) {
                case 'getCheckData':
                    var data = checkStatus.data;
                    var arr = [];
                    if (data.length > 0) {
                        for (var a = 0; a < data.length; a++) {
                            arr.push(data[a].id);
                        }

                        $.ajax({
                            type: "POST",
                            url: "/admin/addId",
                            data: {idArray: arr},
                            success: function (msg) {
                                if(msg.code==200){
                                    alert("添加成功！");
                                }if (msg.code==201){
                                    alert("有的学生已经在班级了");
                                }
                                else {
                                    alert("添加失败！");
                                }

                            }
                        });

                    }
                    break;
                case 'getCheckLength':
                    var data = checkStatus.data;
                    layer.msg('选中了：' + data.length + ' 个');
                    break;
                case 'isAll':
                    layer.msg(checkStatus.isAll ? '全选' : '未全选');
                    break;
            }
            ;
        });

    });
</script>

</body>
</html>